package com.auth.manage.web.config;

import com.auth.manage.web.filter.TokenFilter;
import com.auth.manage.web.handler.GlobalWebExceptionHandler;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
public class WebConfiguration  {
    @Bean
    GlobalWebExceptionHandler globalWebExceptionHandler(){
        return new GlobalWebExceptionHandler();
    }

    // FilterRegistrationBean<TokenFilter>：Spring Boot 提供的一个专门用于注册 Filter 的辅助类
    @Bean
    FilterRegistrationBean<TokenFilter> tokenFilter(StringRedisTemplate redisTemplate){
        FilterRegistrationBean<TokenFilter> filterBean = new FilterRegistrationBean<>();
        filterBean.setFilter(new TokenFilter(redisTemplate));  // 告诉注册器要注册的过滤器是哪个
        filterBean.addUrlPatterns("/buy");  // 过滤器要拦截的路径
        filterBean.setOrder(10);  // 过滤器的顺序，数字越小，优先级越高
        return filterBean;
    }


}
